<?php

	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	
	$action = $_GET['action'];
	$spc = $_GET['spc'];
	if($action != 'acr'){
		show_normal_mesR('操作错误','操作错误','操作错误');
	}
	
	$wjcontent = file_get_contents("http://www.hhcomic.com/comicupdate/");
	getwj($wjcontent);
	
	echo "采集完毕";
	ob_end_flush();
	exit;
	
	
	//以下为函数定义
	
	
	
	function getwj($content){
		
		
		$content = explode('<!-- Baidu Button END -->',$content);
		$content = $content[1];
		$content = explode('  </div></div>',$content);
		$content = $content['0'];
		
		//获取ID
		$match_str = "/<a href=\"\/comic\/(.*)\/\" target=\"_blank\"style/isU";
		preg_match_all($match_str,$content,$urln);
		
		//采集该ID
		foreach ($urln[1] as $v) {
			getsig($v);
		}
	}
	
	function getsig($t){
		$server_url_list = array("http://58.51.95.67:9896/dm01",
				"http://58.51.95.67:9896/dm02",
				"http://58.51.95.67:9896/dm03",
				"http://58.51.95.66:9898/dm04",
				"http://121.12.121.199:9199/dm05",
				"http://58.51.95.67:9896/dm06",
				"http://58.51.95.67:9896/dm07",
				"http://121.12.121.199:9199/dm08",
				"http://58.51.95.67:9896/dm09",
				"http://58.51.95.67:9896/dm10",
				"http://58.51.95.67:9896/dm11",
				"http://121.12.121.199:9199/dm12",
				"http://58.51.95.67:9896/dm13",
				"http://8.8.8.8:99/dm14",
				"http://58.51.95.67:9896/dm15",
				"http://121.12.121.199:9199/dm16"
		);
		global $DB;
		$tid = $t;
		if(!is_numeric($tid) || $tid < 0){
			show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
		}
		$url = 'http://www.hhcomic.com/comic/' . $tid . '/';
		//分离URL以及ID
		$id = $tid;
		//定义公用变量
		if (substr($url,-1)!="/")
			$url.="/";
		$url_pre = 'http://www.hhcomic.com/';
			
			
		//获取URL内容
		$content = 	file_get_contents($url);
		$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
		
		//获取名称
		$name = explode('valign="top" ><b>',$content);
		$name = $name[1];
		$name = explode('内容',$name);
		$name = ADD_addslashes($name[0],true);
		ob_flush();
		echo "现在开始采集" . $name . "<br>";
		
		
		//获取最后更新时间
		$dt = time();
		
		
		//获取URL列表
		$urls = explode('<ul class="bl">',$content);
		$urls = $urls[1];
		$urls = explode('</ul>',$urls);
		$urls = $urls[0];
		
		
		$match_str = "/<li><a href=(.*) target=_blank>/isU";
		preg_match_all($match_str,$urls,$urln);
		$urln = $urln[1];
		//获取每单位总数
		$ulnum = count($urln);
		//进入每页进行处理
		
		//插入分类信息
			//检查当前ID是否存在
			$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'COUNT(*) as max','from'   => 'hhtags','where'	=> "1 " . $where ,)
			);	
			//当已经存在的时候更新日期
			if ($result['max'] > 0) {
				
			}
			//当不存在时插入
			else{
				do_taginsert($id,$dt,$name,0);
			}
		//获取数据库COUNT
		$where = 'AND id= ' . $id .'';
			$result = $DB->build_and_exec_query(
				array('select' => 'count','from'   => 'hhtags','where'	=> "1 " . $where ,)
			);	
		$dbcount = $result['count'];
		
		//由于是逆序，需要倒装数组
		$urln = array_reverse($urln);
		
		
		foreach ($urln as $k=>$v) {
			//当COUNT小于已经采集的时候，跳过。
			if ($k < $dbcount) {
				continue;
			}
			//获取服务器列表号
			$serid = explode('s=',$v);
			$serid = $serid[1];
			//构造图片前缀
			$pic_download_prex = make_pic_url_prex($serid,	$server_url_list);
			$current_url = $url_pre . $v;
			$comic_contents = file_get_contents($current_url);
			//获取所有图片连接
			$purls = explode('var PicListUrls = "',$comic_contents);
			$purls = explode('";',$purls[1]);
			//串行化数组
			$purls = serialize(explode("|", $purls[0]));
			//这里添加数据库插入语句
			do_photoinsert($id,$purls,$pic_download_prex);
		}
		//更新计数
		$where = 'id= ' . $id .'';
		$DB->do_update('hhtags',
			array(					  
			   'lastupdate'				=> $dt,
			   'count'					=> count($urln),
			 ),
			 $where
		);
	}
	
	ob_end_flush();
	exit;
	
	

	function make_pic_url_prex($server_id,$server_list){
		return $server_list[$server_id - 1];
	}
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( 'hhtags', array( 'id'			=> $id,
				'lastupdate'				=> $lu,
				'name'					=> $name,
				'count'					=> $count,
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$prez){
		global $DB;
		$DB->do_insert( 'hhphotos', array(
				'uid'		=> $uid,
				'url'		=> $url,
				'prez'		=> $prez
		)		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	//漫画具体下载地址生成
	
	function make_picurls($base_url,$pages,$prez){
		$urls = "";
		get_pre_nums($prez, 1);
		for ($i = 1; $i < $pages + 1; $i++) {
			$urls .= $base_url . get_pre_nums($prez, $i) . ".jpg\r\n";
		}
		return $urls;
	}
	
	
	
?>